home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / RxSocket / Examples / ps.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  2001-05-24  |  2.5 KB  |  114 lines

  1. /*
  2.  * Name: ps.rexx 6.1 (11.3.2001)
  3.  * Description: fast tcp ports scanner
  4.  *
  5.  * Usage: ps <HOST> [FROM/N] [TO/N] [VER=VERBOSE/S]
  6.  *
  7.  * HOST - the host to scan
  8.  * FROM - from port; 0<FROM<65536 ; default: 1
  9.  * TO - to port; 0<FROM<=TO<65536 ; default: 80
  10.  * VER - verbose mode
  11.  */
  12.  
  13. signal on break_c
  14.  
  15. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  16. call MacroEnv("env","stderr")
  17. if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then exit
  18.  
  19. if ~RMH_ReadArgs("HOST/A,FROM/N,TO/N,VER=VERBOSE/S") then do
  20.     call PrintFault()
  21.     exit
  22. end
  23.  
  24. if parm.1.flag then from=parm.1.value
  25. else from=1
  26.  
  27. if parm.2.flag then to=parm.2.value
  28. else
  29.     if parm.1.flag then to=from
  30.     else to=80
  31. if from<1 | to>65535 | from>to then call err "bad ports sequence" from"..."to
  32.  
  33. sin.addrAddr=resolve(parm.0.value)
  34. if sin.addrAddr=-1 then cal lerr "host <"parm.0.value"> not found"
  35.  
  36. sm=2**AllocSignal()
  37. call SetSocketBaseSingle("SIGEVENTMASK",sm)
  38. ctrl_c=2**12
  39. ctrl_d=2**13
  40.  
  41. ppt=64
  42. t=to-from+1
  43. n=t%ppt
  44. if (t//ppt)>0 then n=n+1
  45.  
  46. if parm.3.flag then
  47.     if IsDotAddr(parm.0.value)
  48.         then call info "Scanning <"parm.0.value">" from"..."to "("n "cycles)"
  49.         else call info "Scanning <"parm.0.value"> ["sin.addrAddr"]" from"..."to "("n "cycles)"
  50.  
  51. do j=1 to n
  52.  
  53.     if (from+ppt)>to then t=to
  54.     else t=from+ppt-1
  55.     todo=t-from+1
  56.  
  57.     do i=from to t
  58.         s=socket(inet,stream)
  59.         if s<0 then call err "can't create socket",1
  60.         call SetSockOpt(s,"SOCKET","EVENTMASK","CONNECT ERROR READ WRITE")
  61.         call IOCtlSocket(s,"FIONBIO",1)
  62.         ports.s=i
  63.         sin.addrPort=i
  64.         call connect(s,"SIN")
  65.  
  66.         srec=SetSignal(0,or(ctrl_c,sm))
  67.         if and(srec,ctrl_c)>0 then signal break_c
  68.         if and(srec,sm)>0 then call getEvent
  69.     end
  70.  
  71.     do while todo>0
  72.         if and(wait(or(ctrl_c,sm)),ctrl_c)>0 then signal break_c
  73.         call getEvent
  74.     end
  75.  
  76.     from=from+ppt
  77. end
  78.  
  79. exit
  80.  
  81. getEvent:
  82.     do forever
  83.         e=GetSocketEvents("EVENTS")
  84.         if e<0 then return
  85.         if events.connect then do
  86.             string=ports.e
  87.             if parm.3.flag then
  88.                 if GetServByPort("SERV",ports.e,"tcp") then string=serv.servName "("ports.e")"
  89.             say string
  90.         end
  91.         call CloseSocket(e)
  92.         todo=todo-1
  93.     end
  94.     /* never reached */
  95.  
  96. info:
  97. parse arg msg
  98.     call writeln("STDERR",env.prg":" msg)
  99.     return
  100.  
  101. err:
  102. parse arg msg,sock
  103.     if nosock=1 then msg=msg "("errorstring()")"
  104.     call info msg
  105.     exit
  106.  
  107. hi:
  108. break_c:
  109.     call info "broken"
  110.     exit
  111.  
  112.  
  113. /*$VER: ps.rexx 6.1 (11.3.2001)*/
  114.